//
//  RequestService+community.swift
//  NDAi
//
//  Created by 丸子 on 2024/8/19.
//

import UIKit
import Alamofire
import Swime
extension RequestService {
 
    func communityList(page:Int, completeClosure: (([CommunityModel]) -> Void)?, failureClosure: ((String?) -> Void)?) {
        let time = getTimestamp()
        let sign = RequestService.instance.generateHash(data: ["systemId":system_id,
                                                               "timestamp":time,
                                                               "page":"\(page)"])
        session.request("\(apiHost)/api/communityPlaza/list",
                        method: .post,
                        parameters: ["timestamp":time, "sign":sign, "page":page], encoding: JSONEncoding.default)
        .responseData { resp in
            self.statRequest(resp)
            switch resp.result{
            case let .success(data):
                let JSON = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                guard let jsonDic = JSON as? [String: Any],
                      let current = jsonDic["code"] as? String, current == "200",
                      let result = jsonDic["result"] as? [String:Any], let data = result["content"] as? [[String: Any]], let model = [CommunityModel].deserialize(from: data) else { return }
                completeClosure?(model.compactMap { $0 } )
            case let .failure(error):
                failureClosure?(error.localizedDescription)
                XLog.d("getActivityMsg", "sync fail", error)
            }
        }
    }
    
    func communityLike(community_id:String, type:Int, completeClosure: (() -> Void)?, failureClosure: ((String?) -> Void)?) {
        let time = getTimestamp()
        let sign = RequestService.instance.generateHash(data: ["systemId":system_id,
                                                               "timestamp":time,
                                                               "type":"\(type)",
                                                               "community_id":community_id])
        session.request("\(apiHost)/api/communityPlaza/like",
                        method: .post,
                        parameters: ["timestamp":time, "sign":sign, "community_id":community_id, "type":type], encoding: JSONEncoding.default)
        .responseData { resp in
            self.statRequest(resp)
            switch resp.result{
            case let .success(data):
                let JSON = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                guard let jsonDic = JSON as? [String: Any],
                      let current = jsonDic["code"] as? String, current == "200",
                      let result = jsonDic["result"] as? [String:Any] else { return }
                completeClosure?()
            case let .failure(error):
                failureClosure?(error.localizedDescription)
                XLog.d("getActivityMsg", "sync fail", error)
            }
        }
    }
    
    func releaseWorks(data_id:String, type:Int, completeClosure: (() -> Void)?, failureClosure: ((String?) -> Void)?) {
        let time = getTimestamp()
        let sign = RequestService.instance.generateHash(data: ["systemId":system_id,
                                                               "timestamp":time,
                                                               "type":"\(type)",
                                                               "data_id":data_id])
        session.request("\(apiHost)/api/communityPlaza/publish",
                        method: .post,
                        parameters: ["timestamp":time, "sign":sign, "data_id":data_id, "type":type], encoding: JSONEncoding.default)
        .responseData { resp in
//            self.statRequest(resp)
            switch resp.result{
            case let .success(data):
                let JSON = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                guard let jsonDic = JSON as? [String: Any],
                      let current = jsonDic["code"] as? String, let msg = jsonDic["msg"] as? String else { return }
                if current == "200" {
                    completeClosure?()
                }else if current == "5002" {
                    failureClosure?(msg)
                } 
            case let .failure(error):
                failureClosure?(error.localizedDescription)
                XLog.d("getActivityMsg", "sync fail", error)
            }
        }
    }
}
